<template>
  <div>
    <el-form :inline="true" @submit.prevent>
      <el-form-item>
        <el-input v-model="query.keyword" :placeholder="t('users.search')" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="query.sortBy" placeholder="排序字段" style="width:140px">
          <el-option :label="t('users.columns.id')" value="id"/>
          <el-option :label="t('users.columns.username')" value="username"/>
          <el-option :label="t('users.columns.email')" value="email"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="query.sortOrder" style="width:120px">
          <el-option label="降序" value="desc"/>
          <el-option label="升序" value="asc"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="load">查询</el-button>
        <el-button @click="openCreate">{{ t('users.add') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="list" style="width: 100%" v-loading="loading">
      <el-table-column prop="id" :label="t('users.columns.id')" width="80"/>
      <el-table-column prop="username" :label="t('users.columns.username')"/>
      <el-table-column prop="email" :label="t('users.columns.email')"/>
      <el-table-column label="操作" width="160">
        <template #default="{ row }">
          <el-button size="small" @click="openEdit(row)">{{ t('users.edit') }}</el-button>
          <el-button size="small" type="danger" @click="remove(row)">{{ t('users.remove') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="query.page"
      v-model:page-size="query.size"
      :total="total"
      :page-sizes="[10,20,50]"
      layout="total, sizes, prev, pager, next"
      :pager-count="5"
      :page-size-options="[10,20,50]"
      :total-text="`共 ${total} 条`"
      :page-size-text="'条/页'"
      :goto-text="'前往'"
      :page-text="'页'"
      :prev-text="'上一页'"
      :next-text="'下一页'"
      @size-change="load"
      @current-change="load"
    />

    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form" label-width="80px">
        <el-form-item :label="t('users.columns.username')">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item :label="t('users.columns.email')">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item v-if="!form.id" label="密码">
          <el-input v-model="form.passwordHash" type="password" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">{{ t('users.cancel') }}</el-button>
        <el-button type="primary" @click="save">{{ t('users.save') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import request from '../utils/request'
import { reactive, ref, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()

const query = reactive({ page: 1, size: 10, keyword: '', sortBy: 'id', sortOrder: 'desc' })
const list = ref([])
const total = ref(0)
const loading = ref(false)

const dialogVisible = ref(false)
const dialogTitle = ref('')
const form = reactive({ id: null, username: '', email: '', passwordHash: '' })

async function load() {
  loading.value = true
  try {
    const { data } = await request.get('/api/users', { params: query })
    list.value = data.data.records
    total.value = data.data.total
  } finally {
    loading.value = false
  }
}

function openCreate() {
  Object.assign(form, { id: null, username: '', email: '', passwordHash: '' })
  dialogTitle.value = '新增用户'
  dialogVisible.value = true
}

function openEdit(row) {
  Object.assign(form, row)
  dialogTitle.value = '编辑用户'
  dialogVisible.value = true
}

async function save() {
  if (!form.id) {
    await request.post('/api/users', form)
  } else {
    await request.put(`/api/users/${form.id}`, form)
  }
  dialogVisible.value = false
  await load()
}

async function remove(row) {
  await request.delete(`/api/users/${row.id}`)
  await load()
}

onMounted(load)
</script>


